package util;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 
 * 常用工具类
 * 
 * */
public class WEBUtils {
	/**
	 * 前台数据ISO8859-1编码转码utf-8
	 * */
	public static String iosToUtf8(String str){
		if(str==null){return null;}
		try {
			return new String(str.getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			System.out.println(e);
			return null;
		}
	}
	
	/***
	 * 返回文件路径的上级目录
	 * @return
	 */
	public static String dirname(String path){
		String prvPath=null;
		Pattern pattern=Pattern.compile("^(.+)\\.+$");
		Matcher matcher=pattern.matcher(path);
		if(matcher.matches()){
			prvPath=matcher.group(0);
		}
		return prvPath;
	}
	
	/**
	 * 指定时间距离现在的间隔时间
	 * */
	private static final Long MS=1000L;
	private static final Long S=60*MS;
	private static final Long M=60*S;
	private static final Long H=24*M;
	private static final Long D=30*H;
	private static final Long MONTH=12*D;
	public static String time2Now(String str){
		Long oldTime=Long.valueOf(str);
		Long now=new Date().getTime();
		Long space=now-oldTime;
		StringBuffer buffer=new StringBuffer();
		if(space<S){
			buffer.append(space/MS).append("秒前");
		}else if(space<M){
			buffer.append(space/S).append("分钟前");
		}else if(space<H){
			buffer.append(space/M).append("小时前");
		}else if(space<D){
			buffer.append(space/H).append("天前");
		}else if(space<MONTH){
			buffer.append(space/D).append("个月前");
		}else{
			buffer.append(space/MONTH).append("年前");
		}
		return buffer.toString();
	}
	
	/**
	 * 生成MD5
	 * */
	public static String md5(String origin) {
		String resultString = null;
		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = MD5.byteArrayToHexString(md.digest(resultString
					.getBytes()));
		} catch (Exception ex) {

		}
		return resultString;
	}
	
	/****
	 * 获得属性的GET方法名
	 * @param name
	 * @return
	 */
	public static String getPropertiesGetName(String name){
		name=name.trim();
		if(name==null || "".equals(name)){
			return null;
		}
		return "get"+name.substring(0, 1).toUpperCase()+name.substring(1);
	}
	/****
	 * 获得属性的SET方法名
	 * @param name
	 * @return
	 */
	public static String getPropertiesSetName(String name){
		name=name.trim();
		if(name==null || "".equals(name)){
			return null;
		}
		return "set"+name.substring(0, 1).toUpperCase()+name.substring(1);
	}

	/**
	 * 查看
	 * @param id
	 * @param object
	 * @param i
	 * @return
	 */
	public static boolean isNotIn(Object obj, Object... values) {
		Boolean flag=true;
		if(values!=null && values.length>0){
			for (Object value : values) {
				if(obj==null){
					if(obj==value){
						flag=false;
						break;
					}
				}else{
					if(obj.equals(value)){
						flag=false;
						break;
					}
				}
			}
		}else{
			flag=false;
		}
		
		return flag;
	}
	
	/**
	 * 判断字符是否为null,为空字符
	 * @param obj
	 * @return
	 */
	public static Boolean isBlank(String obj){
		return obj==null || "".equals(obj);
	}
	
	/****
	 * 通过指定的属性名从目标model中取出相应表单数据，然后通过Map集合返回
	 * @param model
	 * @param propertiesNames 
	 * @return	map集合<propertiesName,propertiesValue>
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Map<String, Object> buildUpdateDatas(Object model,String... propertiesNames) {
		Class clazz=model.getClass();
		Map<String,Object> datas=new HashMap<>();
		try {
			for (String name: propertiesNames) {
				Method method =clazz.getMethod(WEBUtils.getPropertiesGetName(name));
				Object value=method.invoke(model);
				datas.put(name, value);
			}
		} catch (Exception e) {
			System.err.println("buildUpdateDatas 出错了:"+e);
			return null;
		}
		return datas;
	}
	
	
}

class MD5 {
	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

	/**
	 * 转换字节数组为16进制字串
	 * 
	 * @param b
	 *            字节数组
	 * @return 16进制字串
	 */

	public static String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i]));
		}
		return resultSb.toString();
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n = 256 + n;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}
}